package com.wm.task1;

import com.wm.task1.dao.MessageDAO;
import com.wm.task1.vo.Massage;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class MainView {
    private Scanner sc = new Scanner(System.in);
    private MessageDAO messageDAO = new MessageDAO();
    private List<Massage> massageList = new ArrayList<>();

    public void printMenu() {
        System.out.println("=====留言板=====");
        System.out.println("1、查看留言");
        System.out.println("2、添加留言");
        System.out.println("3、查找留言");
        System.out.println("0、退出");
        System.out.print("请选择:");
    }

    public int choose() {
        return sc.nextInt();
    }

    public int chooseMenu(int choose) {
        switch (choose) {
            case 1:  //1、查看留言

                lookMassage();
                break;
            case 2:  //2、添加留言

                addMassage();
                break;
            case 3:  //3、查找留言
                findMassage();
                break;
            case 0:  //0、退出
                System.out.println("谢谢惠顾,欢迎下次使用!");
                return 0;
            default:
                System.out.println("功能暂未开发,请重试!");
                return 1;
        }
        return 1;
    }

    private void findMassage() {
        System.out.println("=====查找留言=====");
        System.out.print("输入昵称:");
        String name = sc.next();
        massageList = messageDAO.findMassage(name);
        if(massageList == null){
            System.out.println("未找到该用户!");
            return;
        }
        for (Massage massage : massageList) {
            System.out.print(massage.getId() + ".");
            System.out.println(massage.getName());
            System.out.println(massage.getContent());
        }
    }

    private void addMassage() {
        System.out.println("=====添加留言=====");
        System.out.print("请输入昵称");
        String name = sc.next();
        System.out.print("请输入留言内容");
        String content = sc.next();
        messageDAO.addMassage(name,content);
        System.out.println("添加成功");
    }

    private void lookMassage() {
        System.out.println("=====查看留言=====");
        massageList = messageDAO.lookMassage();
        for (Massage massage : massageList) {
            System.out.print(massage.getId() + ".");
            System.out.println(massage.getName());
            System.out.println(massage.getContent());
        }
    }
}

